﻿using NAudio.Wave;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace 文件下MP3时长获取
{
    internal class Program
    {

        public class Mp3FileInfo
        {
            public string FilePath { get; set; }
            public string FileName { get; set; }
            /// <summary>
            /// 真实时长ms+100
            /// </summary>
            public int Duration { get; set; }
            public int Prefix { get; set; }
            /// <summary>
            /// 语音编码
            /// </summary>
            public string SpeechCode { get; set; }
        }
        public class Mp3Scanner
        {
            public List<Mp3FileInfo> ScanMp3Files(string folderPath)
            {
                var mp3Files = new List<Mp3FileInfo>();

                try
                {
                    // 获取文件夹下所有mp3文件
                    var files = Directory.GetFiles(folderPath, "*.mp3", SearchOption.AllDirectories);


                    foreach (var file in files)
                    {
                        var prefix = GetFilePrefix(Path.GetFileName(file));
                        try
                        {
                            using (var reader = new Mp3FileReader(file))
                            {
                                mp3Files.Add(new Mp3FileInfo
                                {
                                    FilePath = file,
                                    FileName = Path.GetFileName(file),
                                    Duration = (int)reader.TotalTime.TotalMilliseconds + 100,
                                    Prefix = prefix,
                                    SpeechCode = $"0A,03,05,12,12,00,00,00,{prefix.ToString("X2")},5D,C3"
                                });
                            }
                        }
                        catch (Exception ex)
                        {
                            // 处理单个文件读取异常
                            Console.WriteLine($"读取文件 {file} 失败: {ex.Message}");
                        }
                    }
                    mp3Files = mp3Files.OrderBy(x => x.Prefix).ToList();
                    var options = new JsonSerializerOptions
                    {
                        WriteIndented = true,  // 美化输出格式
                        Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping  // 处理中文
                    };


                    File.WriteAllText("mp3.json", JsonSerializer.Serialize(mp3Files, options));
                }
                catch (Exception ex)
                {
                    // 处理文件夹访问异常
                    Console.WriteLine($"访问文件夹失败: {ex.Message}");
                }

                return mp3Files;
            }



        }
        public static string bytesToHexString(string bytes)
        {
            string code = $"0A,03,05,12,12,00,00,00,{bytes},5D,C3";
            return code;
        }

        private static int GetFilePrefix(string fileName)
        {
            try
            {
                // 提取文件名开头的数字
                string numberStr = "";
                foreach (char c in fileName)
                {
                    if (char.IsDigit(c))
                    {
                        numberStr += c;
                    }
                    else
                    {
                        break;
                    }
                }

                if (int.TryParse(numberStr, out int prefix))
                {
                    return prefix;
                }
            }
            catch
            {
                return int.MaxValue;
            }
            return int.MaxValue;
        }
        static void Main(string[] args)
        {
            var scanner = new Mp3Scanner();
            var mp3Files = scanner.ScanMp3Files(@"C:\Users\humengjian\Desktop\2024.12.12语音文件包");

            foreach (var mp3 in mp3Files)
            {
                Console.WriteLine($"文件: {mp3.FileName}");
                Console.WriteLine($"路径: {mp3.FilePath}");
                Console.WriteLine($"时长: {mp3.Duration}");
                Console.WriteLine($"编码: {mp3.SpeechCode}");
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}
